package com.ydc.ln.tutorial.j2se.test;

import java.util.StringTokenizer;

import org.junit.Test;

/**
 * <pre>
 * 1.字符串分割
 * 2.长字符串省略
 * 3.字符串组装
 * </pre>
 * 
 * @author yangdc
 * 
 */
public class StringDemo {

	public static void main(String[] args) {
		String strNum = "0123456789";
		System.out.println(strNum.substring(1));
	}

	/**
	 * 长字符串省略
	 * 
	 * <pre>
	 * 英文字符字节宽度为1
	 * 中文字符字节宽度为2
	 * </pre>
	 */
	public String truncate(String source, int len, String delim) {
		if (source == null) {
			return source;
		}
		int delimLen = delim.length();
		if (len <= delimLen) {
			return source;
		}
		int start, stop;
		int alen = source.length();
		if (len > 0) {
			if (alen <= (len - delimLen)) {
				return source;
			}
			start = 0;
			stop = len - delimLen;
			StringBuffer sb = new StringBuffer(source.substring(start, stop));
			sb.append(delim);
			return sb.toString();
		}
		return source;
	}

	/**
	 * 长字符串省略 改进 中英文同长
	 * @len 英文字母的个数 len = 2 代表一个汉字
	 * <pre>
	 * 英文字符字节宽度为1
	 * 中文字符字节宽度为2
	 * </pre>
	 */
	public String truncate2(String source, int len, String delim) {
		if (source == null) {
			return source;
		}
		int delimLen = delim.length();
		if (len <= delimLen) {
			return source;
		}
		int start, stop, byteLen;
		int alen = source.getBytes().length;
		if (len > 0) {
			if (alen <= (len - delimLen)) {
				return source;
			}
			start = stop = byteLen = 0;
			while (byteLen <= (len - delimLen)) {
				if (source.substring(stop, stop + 1).getBytes().length == 1) {
					byteLen += 1;
				} else {
					byteLen += 2;
				}
				stop++;
			}
			;
			StringBuffer sb = new StringBuffer(source.substring(start, stop - 1));
			sb.append(delim);
			return sb.toString();
		}
		return source;
	}

	/**
	 * 字符串分割
	 * 
	 * <pre>
	 * 测试StringTokenizer
	 * split:根据正则表达式分割字符串 
	 * StringTokenizer:按照给定的分割符拆分字符串
	 * </pre>
	 */
	@Test
	public void testStringTokenizer() {
		String str = "如何，分割，字符串：方法一，方法二";
		StringTokenizer st = new StringTokenizer(str, "，：");
		while (st.hasMoreElements()) {
			System.out.println(st.nextToken());
		}
	}

	/**
	 * 字符串组装
	 * 
	 * <pre>
	 * 测试StringBuffer字符串组装
	 * </pre>
	 */
	@Test
	public void testStringBuffer() {
		StringBuffer sql = new StringBuffer();
		sql.append("in (");
		for (int i = 0; i < 3; i++) {
			sql.append(i + ",");
		}
		sql.deleteCharAt(sql.length() - 1);
		sql.append(")");
		System.out.println(sql);
	}

	/**
	 * 测试字符串数组的合并
	 */
	@Test
	public void testStringArray() {
		String[] aArray = { "a1", "a2" };
		String[] bArray = { "b1", "b2", "b3" };
		String[] cArray = { "c1" };
	}

	/**
	 * 为什么写成"".equals()
	 */
	@Test
	public void testEquals() {
		String str = null;
		System.out.println("".equals(str));
		System.out.println(str.equals(""));
	}

	/**
	 * hash碰撞
	 */
	@Test
	public void testHashCode() {
		String str1 = "Aa";
		String str2 = "BB";
		System.out.println(str1.hashCode());
		System.out.println(str2.hashCode());
	}

	@Test
	public void testEmptyString() {
		String str = "";
		Float flo = null;
		try {
			flo = Float.valueOf(str);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
	}

	@Test
	public void testStrObj() {
		String a = "abc";
		String aS = new String(a);
		System.out.println(a == aS);
	}

	public String reverse(String str) {
		for (int i = 0; i < str.length() / 2; i++) {
			char c = str.charAt(str.length());

		}
		return str;
	}

	/**
	 * <pre>
	 * String 的split()方法
	 * 如果分隔符中含有元字符则须转义
	 * 如：
	 * \.	表示 .
	 * </pre>
	 */
	@Test
	public void testSplit() {
		String SPLIT = "\\.jpg";
		// "}else if(adres == \"jpg\" || adres == \"gif\" || adres == \"png\" || adres == \"jpeg\" ){"
		String str = "abc.jpgdefg";
		String[] strArray = str.split(SPLIT);
		for (String _str : strArray) {
			System.out.println(_str);
		}
	}

	/**
	 * 
	 */
	@Test
	public void testSubstring() {
		String str = "http://findicons.com/search/mail";
		System.out.println(str.indexOf("."));
		System.out.println(str.lastIndexOf("."));
		System.out.println(str.indexOf("http://"));
	}
}
